package br.uefs.ecomp.myBook.model;

import static org.junit.Assert.*;

import java.io.*;

import org.junit.Before;
import org.junit.Test;

import br.uefs.ecomp.myBook.control.UserController;
import br.uefs.ecomp.myBook.util.DataAlreadyExistsException;

public class PerfilTest {

	Profile p1, p2, p3;

	@Before
	public void setUp() throws Exception {
		p1 = new Profile("joaozinho", "12345", "Joao da Silva", "88888888",
				"joaozinho@silva.com", /*"Rua x, y",*/ "City", "05-12-1999");
		p2 = new Profile("pedrinho", "54321", "Pedro da Silva", "88888888",
				"pedrinho@silva.com", /*"Rua x, y",*/ "City", "05-12-1999");
		p3 = new Profile("mariazinha", "54321", "Pedro da Silva", "88888888",
				"pedrinho@silva.com", /*"Rua x, y",*/ "City", "05-12-1999");
		
	}

	@Test
	public void testSaveProfile() throws IOException {
		p1.saveProfile();
		p2.saveProfile();

		
		/*
		try {
			UserController.getInstance().signUp("joaozinho", "12345", "Joao da Silva", "88888888",
					"joaozinho@silva.com", "Rua x, y", "City", "05-12-1999");
			MyBookSystem.commit();
		} catch (DataAlreadyExistsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		p3 = Profile.loadProfile("pedrinho");
		assertNotNull(p3);
		assertEquals(p2, p3);
	}

	@Test
	public void testCompareTo() {
		assertTrue(p1.compareTo(p2) < 0);
		assertTrue(p2.compareTo(p1) > 0);
		assertTrue(p1.compareTo(p1) == 0);
	}
	
	@Test
	public void testRequests(){
		p1.addRequest(p2);
		p1.addRequest(p3);
		p2.addRequest(p3);
		
		assertTrue(p1.isRequesting(p2));
		assertTrue(p2.isRequestedBy(p1));
		p3.deleteRequestor(p1);
		assertFalse(p1.isRequesting(p3));
		assertFalse(p3.isRequestedBy(p1));
		
		
	}
	
	@Test
	public void testMatches(){
		assertTrue(p1.matches("Silva"));
		assertFalse(p2.matches("Joao"));
		assertTrue(p1.matches("Jo\nCi", Profile.SEARCH_NAME_CITY));
		assertFalse(p1.matches("ao\ty", Profile.SEARCH_NAME_CITY));
	}

}
